package com.plian.system.task.userInfo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.plian.system.entity.fa.nplcancelmaintain.CqzcNplcancelmaintain;
import com.plian.system.entity.hr.staff.*;
import com.plian.system.entity.hr.staffAnnualBackup.*;
import com.plian.system.mapper.hr.hrbackup.*;
import com.plian.system.mapper.hr.staff.AppointRemoveMapper;
import com.plian.system.mapper.hr.staff.StaffFilesMapper;
import com.plian.system.model.R;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.service.hr.staff.*;
import com.plian.system.vo.hr.staff.*;
import com.plian.system.wrapper.hr.staff.StaffFilesWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**定时弹窗
 * @Author：zsk
 * @Package：com.plian.system.task.rectificationSituation
 * @Project：xt-gzw
 * @name：RectificationSituationTask
 * @Date：2025/2/17 9:55
 * @Filename：RectificationSituationTask
 */
@Configuration
public class StafffilesTask {
    private static final Logger log = LoggerFactory.getLogger(StafffilesTask.class);

    @Resource
    private StaffFilesOldMapper staffFilesOldMapper;
    @Resource
    private StaffFilesMapper staffFilesMapper;
    @Resource
    private AnnualAssessmentOldMapper annualAssessmentOldMapper;
    @Resource
    private AppointRemoveOldMapper appointRemoveOldMapper;
    @Resource
    private EducationDegreeOldMapper educationDegreeOldMapper;
    @Resource
    private OverseasMembersOldMapper overseasMembersOldMapper;
    @Resource
    private PractitionerCertificateOldMapper practitionerCertificateOldMapper;
    @Resource
    private RewardsPunishmentsOldMapper rewardsPunishmentsOldMapper;
    @Resource
    private SocialRelationsOldMapper socialRelationsOldMapper;
    @Resource
    private TechnicalQualificationOldMapper technicalQualificationOldMapper;
    @Resource
    private TrainOldMapper trainOldMapper;
    @Resource
    private WorkExperienceOldMapper workExperienceOldMapper;
    @Resource
    private IAppointRemoveService appointRemoveService;
    @Resource
    private IEducationDegreeService educationDegreeService;
    @Resource
    private IWorkExperienceService workExperienceService;
    @Resource
    private IRewardsPunishmentsService rewardsPunishmentsService;
    @Resource
    private IAnnualAssessmentService annualAssessmentService;
    @Resource
    private ISocialRelationsService socialRelationsService;
    @Resource
    private IPractitionerCertificateService practitionerCertificateService;
    @Resource
    private ITechnicalQualificationService technicalQualificationService;
    @Resource
    private ITrainService trainService;
    @Resource
    private IOverseasMembersService overseasMembersService;
    @Scheduled(cron = " 0 0 0 1 1 ? ")
    private void configureTasks() {
        LocalDate currentDate = LocalDate.now(); // 获取当前日期
        int currentYear = currentDate.getYear(); // 获取当前年份
        String lastYearStr=String.valueOf(currentYear-1);//去年
        List<StaffFiles> staffFilesList=staffFilesMapper.selectList(null);
        if(staffFilesList!=null&&staffFilesList.size()>0){
            for (StaffFiles staffFiles: staffFilesList) {
                if(staffFiles!=null){
                    StaffFilesOld staffFilesOld=new StaffFilesOld();
                    BeanUtils.copyProperties(staffFiles, staffFilesOld);
                    staffFilesOld.setId(null);
                    staffFilesOld.setNewId(staffFiles.getId());
                    staffFilesOld.setBackYear(String.valueOf(currentYear-1));
                    int staffResult=staffFilesOldMapper.insert(staffFilesOld);
                    if(staffResult>0&&staffFilesOld!=null&&staffFilesOld.getId()!=null){
                        List<AnnualAssessment> annualAssessmentList=annualAssessmentService.list(new LambdaQueryWrapper<AnnualAssessment>().eq(AnnualAssessment::getStaffFilesId, staffFiles.getId()));
                        if(annualAssessmentList!=null&&annualAssessmentList.size()>0){//年度考核结果
                            for(AnnualAssessment annualAssessment:annualAssessmentList){
                                AnnualAssessmentOld annualAssessmentOld=new AnnualAssessmentOld();
                                BeanUtils.copyProperties(annualAssessment,annualAssessmentOld);
                                annualAssessmentOld.setId(null);
                                annualAssessmentOld.setNewId(annualAssessment.getId());
                                annualAssessmentOld.setBackYear(lastYearStr);
                                annualAssessmentOld.setStaffFilesId(staffFilesOld.getId());
                                annualAssessmentOldMapper.insert(annualAssessmentOld);
                            }
                        }
                        List<AppointRemove> appointRemoveList=appointRemoveService.list(new LambdaQueryWrapper<AppointRemove>().eq(AppointRemove::getStaffFilesId, staffFiles.getId()));
                        if(appointRemoveList!=null&&appointRemoveList.size()>0){//任免记录
                            for(AppointRemove entity:appointRemoveList){
                                AppointRemoveOld entityOld=new AppointRemoveOld();
                                BeanUtils.copyProperties(entity,entityOld);
                                entityOld.setId(null);
                                entityOld.setNewId(entity.getId());
                                entityOld.setBackYear(lastYearStr);
                                entityOld.setStaffFilesId(staffFilesOld.getId());
                                appointRemoveOldMapper.insert(entityOld);
                            }
                        }
                        List<EducationDegree> educationDegreeList=educationDegreeService.list(new LambdaQueryWrapper<EducationDegree>().eq(EducationDegree::getStaffFilesId, staffFiles.getId()));
                        if(educationDegreeList!=null&&educationDegreeList.size()>0){//学历学位
                            for(EducationDegree entity:educationDegreeList){
                                EducationDegreeOld entityOld=new EducationDegreeOld();
                                BeanUtils.copyProperties(entity,entityOld);
                                entityOld.setId(null);
                                entityOld.setNewId(entity.getId());
                                entityOld.setBackYear(lastYearStr);
                                entityOld.setStaffFilesId(staffFilesOld.getId());
                                educationDegreeOldMapper.insert(entityOld);
                            }
                        }
                        List<OverseasMembers> overseasMembersList=overseasMembersService.list(new LambdaQueryWrapper<OverseasMembers>().eq(OverseasMembers::getStaffFilesId, staffFiles.getId()));
                        if(overseasMembersList!=null&&overseasMembersList.size()>0){//侨眷情况
                            for(OverseasMembers entity:overseasMembersList){
                                OverseasMembersOld entityOld=new OverseasMembersOld();
                                BeanUtils.copyProperties(entity,entityOld);
                                entityOld.setId(null);
                                entityOld.setNewId(entity.getId());
                                entityOld.setBackYear(lastYearStr);
                                entityOld.setStaffFilesId(staffFilesOld.getId());
                                overseasMembersOldMapper.insert(entityOld);
                            }
                        }
                        List<PractitionerCertificate> practitionerCertificateList=practitionerCertificateService.list(new LambdaQueryWrapper<PractitionerCertificate>().eq(PractitionerCertificate::getStaffFilesId, staffFiles.getId()));
                        if(practitionerCertificateList!=null&&practitionerCertificateList.size()>0){//从业资格证书
                            for(PractitionerCertificate entity:practitionerCertificateList){
                                PractitionerCertificateOld entityOld=new PractitionerCertificateOld();
                                BeanUtils.copyProperties(entity,entityOld);
                                entityOld.setId(null);
                                entityOld.setNewId(entity.getId());
                                entityOld.setBackYear(lastYearStr);
                                entityOld.setStaffFilesId(staffFilesOld.getId());
                                practitionerCertificateOldMapper.insert(entityOld);
                            }
                        }
                        List<RewardsPunishments> rewardsPunishmentsList=rewardsPunishmentsService.list(new LambdaQueryWrapper<RewardsPunishments>().eq(RewardsPunishments::getStaffFilesId, staffFiles.getId()));
                        if(rewardsPunishmentsList!=null&&rewardsPunishmentsList.size()>0){//奖惩情况
                            for(RewardsPunishments entity:rewardsPunishmentsList){
                                RewardsPunishmentsOld entityOld=new RewardsPunishmentsOld();
                                BeanUtils.copyProperties(entity,entityOld);
                                entityOld.setId(null);
                                entityOld.setNewId(entity.getId());
                                entityOld.setBackYear(lastYearStr);
                                entityOld.setStaffFilesId(staffFilesOld.getId());
                                rewardsPunishmentsOldMapper.insert(entityOld);
                            }
                        }
                        List<SocialRelations> socialRelationsList=socialRelationsService.list(new LambdaQueryWrapper<SocialRelations>().eq(SocialRelations::getStaffFilesId, staffFiles.getId()));
                        if(socialRelationsList!=null&&socialRelationsList.size()>0){//家庭及社会关系
                            for(SocialRelations entity:socialRelationsList){
                                SocialRelationsOld entityOld=new SocialRelationsOld();
                                BeanUtils.copyProperties(entity,entityOld);
                                entityOld.setId(null);
                                entityOld.setNewId(entity.getId());
                                entityOld.setBackYear(lastYearStr);
                                entityOld.setStaffFilesId(staffFilesOld.getId());
                                socialRelationsOldMapper.insert(entityOld);
                            }
                        }
                        List<TechnicalQualification> technicalQualificationList=technicalQualificationService.list(new LambdaQueryWrapper<TechnicalQualification>().eq(TechnicalQualification::getStaffFilesId, staffFiles.getId()));
                        if(technicalQualificationList!=null&&technicalQualificationList.size()>0){//专业技术资格
                            for(TechnicalQualification entity:technicalQualificationList){
                                TechnicalQualificationOld entityOld=new TechnicalQualificationOld();
                                BeanUtils.copyProperties(entity,entityOld);
                                entityOld.setId(null);
                                entityOld.setNewId(entity.getId());
                                entityOld.setBackYear(lastYearStr);
                                entityOld.setStaffFilesId(staffFilesOld.getId());
                                technicalQualificationOldMapper.insert(entityOld);
                            }
                        }
                        List<Train> trainList=trainService.list(new LambdaQueryWrapper<Train>().eq(Train::getStaffFilesId, staffFiles.getId()));
                        if(trainList!=null&&trainList.size()>0){//近五年三月以上培训情况
                            for(Train entity:trainList){
                                TrainOld entityOld=new TrainOld();
                                BeanUtils.copyProperties(entity,entityOld);
                                entityOld.setId(null);
                                entityOld.setNewId(entity.getId());
                                entityOld.setBackYear(lastYearStr);
                                entityOld.setStaffFilesId(staffFilesOld.getId());
                                trainOldMapper.insert(entityOld);
                            }
                        }
                        List<WorkExperience> workExperienceList= workExperienceService.list(new LambdaQueryWrapper<WorkExperience>().eq(WorkExperience::getStaffFilesId, staffFiles.getId()));
                        if(workExperienceList!=null&&workExperienceList.size()>0){//近五年三月以上培训情况
                            for(WorkExperience entityVo:workExperienceList){
                                WorkExperienceOld entityOld=new WorkExperienceOld();
                                BeanUtils.copyProperties(entityVo,entityOld);
                                entityOld.setId(null);
                                entityOld.setNewId(entityVo.getId());
                                entityOld.setBackYear(lastYearStr);
                                entityOld.setStaffFilesId(staffFilesOld.getId());
                                workExperienceOldMapper.insert(entityOld);
                            }
                        }
                    }
                }
            }
        }
    }
}
